var t = wx.createCanvasContext("waterMarkCanves"), e = wx.createCanvasContext("waterMarkCanves2"), a = wx.createCanvasContext("waterMarkCanves3"), i = wx.createCanvasContext("waterMarkCanves4");

Page({
    data: {
        imageWidth: "",
        imageHeight: "",
        imagesArr: [],
        textToPrint: "",
        rotate: "45",
        color: "black",
        alpha: 50
    },
    onLoad: function(t) {
        var e = JSON.parse(t.imagesArr);
        this.setData({
            imagesArr: e,
            textToPrint: t.textToPrint,
            color: t.color,
            rotate: t.rotate,
            fontSize: t.fontSize,
            alpha: t.alpha
        });
    },
    onReady: function() {
        var o = this, n = o.data, h = n.imagesArr;
        o.isInArray(h, "") && h.pop();
        for (var s = 0; s < h.length; s++) wx.getImageInfo({
            src: h[s],
            success: function(s) {
                console.log(s);
                var r, g = s.width, l = s.height, c = wx.getSystemInfoSync().windowWidth;
                g >= l ? (g > c && (g = c), l = l / s.width * g) : (g > c && (g = c), l > 400 ? (l = 400, 
                g = s.width / s.height * l) : l = l / s.width * g), s.path == h[0] && (o.setData({
                    imageWidth: g,
                    imageHeight: l
                }), r = t), s.path == h[1] && (o.setData({
                    imageWidth2: g,
                    imageHeight2: l
                }), r = e), s.path == h[2] && (o.setData({
                    imageWidth3: g,
                    imageHeight3: l
                }), r = a), s.path == h[3] && (o.setData({
                    imageWidth4: g,
                    imageHeight4: l
                }), r = i), r.drawImage(s.path, 0, 0, g, l), r.rotate(n.rotate * Math.PI / 180);
                for (var m = 1; m < 12; m++) {
                    r.beginPath(), r.setFontSize(n.fontSize), r.setFillStyle(n.color), r.globalAlpha = n.alpha / 100, 
                    r.fillText(n.textToPrint, 0, 50 * m);
                    for (var d = 1; d < 12; d++) r.beginPath(), r.setFontSize(n.fontSize), r.setFillStyle(n.color), 
                    r.globalAlpha = n.alpha / 100, r.fillText(n.textToPrint, (15 + (n.fontSize - 1) * n.textToPrint.length) * d, 50 * m);
                }
                for (var f = 0; f < 12; f++) {
                    r.beginPath(), r.setFontSize(n.fontSize), r.setFillStyle(n.color), r.fillText(n.textToPrint, 0, -50 * f);
                    for (var w = 1; w < 12; w++) r.beginPath(), r.setFontSize(n.fontSize), r.setFillStyle(n.color), 
                    r.fillText(n.textToPrint, (15 + (n.fontSize - 1) * n.textToPrint.length) * w, -50 * f);
                }
                r.draw();
            }
        });
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    isInArray: function(t, e) {
        for (var a = 0; a < t.length; a++) if (e === t[a]) return !0;
        return !1;
    },
    toSavePic: function() {
        var t = this.data, e = t.imagesArr;
        this.isInArray(e, "") && e.pop(), wx.showLoading({
            title: "保存中..."
        });
        for (var a = 0; a < e.length; a++) 0 == a && wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: t.imageWidth,
            height: t.imageHeight,
            destWidth: 2 * t.imageWidth,
            destHeight: 2 * t.imageHeight,
            canvasId: "waterMarkCanves",
            success: function(t) {
                wx.saveImageToPhotosAlbum({
                    filePath: t.tempFilePath
                }), wx.hideLoading(), wx.showToast({
                    title: "保存成功！",
                    icon: "success"
                }), setTimeout(function() {
                    wx.navigateBack({});
                }, 1e3);
            }
        }), 1 == a && wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: t.imageWidth2,
            height: t.imageHeight2,
            destWidth: 2 * t.imageWidth2,
            destHeight: 2 * t.imageHeight2,
            canvasId: "waterMarkCanves2",
            success: function(t) {
                wx.saveImageToPhotosAlbum({
                    filePath: t.tempFilePath
                });
            }
        }), 2 == a && wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: t.imageWidth3,
            height: t.imageHeight3,
            destWidth: 2 * t.imageWidth3,
            destHeight: 2 * t.imageHeight3,
            canvasId: "waterMarkCanves3",
            success: function(t) {
                wx.saveImageToPhotosAlbum({
                    filePath: t.tempFilePath
                });
            }
        }), 3 == a && wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: t.imageWidth4,
            height: t.imageHeight4,
            destWidth: 2 * t.imageWidth4,
            destHeight: 2 * t.imageHeight4,
            canvasId: "waterMarkCanves4",
            success: function(t) {
                wx.saveImageToPhotosAlbum({
                    filePath: t.tempFilePath
                });
            }
        });
    }
});